package com.steven.ourcontest.forum.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.steven.ourcontest.R
import com.steven.ourcontest.base.BaseViewBinder
import com.steven.ourcontest.forum.bean.ForumContentBean
import com.steven.ourcontest.me.widget.GlideRoundTransform
import com.steven.ourcontest.net.NetConstants
import com.steven.ourcontest.utils.DateUtil

class ForumContentViewBinder(
    private val context: Context?,
    private val itemClickListener: ((fid: Long) -> Unit)
) : BaseViewBinder<ForumContentBean, ViewHolder>() {
    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val mInflater = inflater.inflate(R.layout.item_forum_list, parent, false)
        return ViewHolder(mInflater)
    }

    override fun onBindViewHolder(itemHolder: ViewHolder, itemData: ForumContentBean) {
        itemHolder.apply {
            tv_name?.text = itemData.uname
            tv_title?.text = itemData.title
            tv_content?.text = itemData.content
            tv_readNum?.text = itemData.readNum.toString()
            tv_discussNum?.text = itemData.discussNum.toString()
            when (getPosition(itemHolder)) {
                0 -> {
                    view_start?.isVisible = true
                }
                else -> {
                    view_start?.isVisible = false
                }
            }
            tv_time?.text = DateUtil.timeStamp2Date(itemData.createTime, "yyyy-MM-dd")
            itemView.setOnClickListener {
                itemClickListener.invoke(itemData.fid)
            }
            if (context != null) {
                if (itemData.iconUrl != null) {
                    Glide.with(context)
                        .load(NetConstants.ICON_BASE_URL + itemData.iconUrl)
                        .placeholder(R.drawable.ic_placeholder)
                        .error(R.drawable.aio_image_fail)
                        .apply(RequestOptions.bitmapTransform(
                            GlideRoundTransform(
                                context,
                                10
                            )
                        ))
                        .into(iv_icon)
                }
            }
        }
    }
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val view_start: View? = itemView.findViewById(R.id.view_start)
    val iv_icon: ImageView = itemView.findViewById(R.id.iv_icon)
    val tv_name: TextView? = itemView.findViewById(R.id.tv_name)
    val tv_title: TextView? = itemView.findViewById(R.id.tv_title)
    val tv_content: TextView? = itemView.findViewById(R.id.tv_content)
    val tv_readNum: TextView? = itemView.findViewById(R.id.tv_readNum)
    val tv_discussNum: TextView? = itemView.findViewById(R.id.tv_discussNum)
    val tv_time: TextView? = itemView.findViewById(R.id.tv_time)
}